//---------------------------------------------------------------------------
#include "MyINIManager.h"
#ifdef MY_BORLAND
#include <vcl.h>
#pragma hdrstop
#pragma package(smart_init)
//---------------------------------------------------------------------------
static inline void ValidCtrCheck(MyIniManager *)
{
	new MyIniManager(NULL);
}
#else
#include <stdarg.h>
#include <windef.h>
#include <winbase.h>
#endif

//---------------------------------------------------------------------------
MyIniManager::MyIniManager(char *FileName)
{
	int Len = strlen(FileName);
	FFileName = (char*)malloc(Len + 1);
	memset(FFileName,0,Len+1);
	memcpy(FFileName,FileName,Len);
}
//---------------------------------------------------------------------------
void MyIniManager::WriteInteger(char* szSection, char* szKey, int iValue)
{
	char szValue[255];
	sprintf(szValue, "%d", iValue);
#ifdef UNICODE
	WritePrivateProfileString(char_to_wchar(szSection),  char_to_wchar(szKey), 
														char_to_wchar(szValue), char_to_wchar(FFileName));
#else
	WritePrivateProfileString(szSection,  szKey, szValue, FFileName);
#endif	
}
//---------------------------------------------------------------------------
void MyIniManager::WriteHex(char* szSection, char* szKey, int iValue)
{
	char szValue[255];
	sprintf(szValue, "%X", iValue);
#ifdef UNICODE
	WritePrivateProfileString(char_to_wchar(szSection),  char_to_wchar(szKey), 
														char_to_wchar(szValue), char_to_wchar(FFileName));
#else
	WritePrivateProfileString(szSection,  szKey, szValue, FFileName);
#endif	
}
//---------------------------------------------------------------------------
void MyIniManager::WriteString(char* szSection, char* szKey, char* szValue)
{
	LPCTSTR LSection;
#ifdef UNICODE	
	WritePrivateProfileString(char_to_wchar(szSection),  char_to_wchar(szKey), 
														char_to_wchar(szValue), char_to_wchar(FFileName));
#else
	WritePrivateProfileString(szSection,  szKey, szValue, FFileName);
#endif		
}
//---------------------------------------------------------------------------
 void MyIniManager::WriteBool(char* szSection, char* szKey, bool bolValue)
{
	char szValue[255];
	sprintf(szValue, "%s", bolValue ? "True" : "False");
	LPCTSTR LSection;
#ifdef UNICODE	
	WritePrivateProfileString(char_to_wchar(szSection),  char_to_wchar(szKey), 
														char_to_wchar(szValue), char_to_wchar(FFileName));
#else
	WritePrivateProfileString(szSection,  szKey, szValue, FFileName);
#endif				
}
//---------------------------------------------------------------------------
int MyIniManager::ReadInteger(char* szSection, char* szKey, int iDefaultValue)
{
	int iResult;

#ifdef UNICODE
	wchar_t *wSection = char_to_wchar(szSection);
	wchar_t *wKey = char_to_wchar(szKey);
	wchar_t *wFile = char_to_wchar(FFileName);
	iResult = GetPrivateProfileInt(wSection,  wKey,
					iDefaultValue, wFile);
#else
	iResult = GetPrivateProfileInt(szSection,  szKey, iDefaultValue, FFileName);
#endif
	return iResult;
}
//---------------------------------------------------------------------------
int MyIniManager::ReadHex(char* szSection, char* szKey, int iDefaultValue)
{
	int iResult;
	char *Temp = ReadString(szSection,szKey,"0");
	char *Val = (char*)malloc(strlen(Temp) + 0x10);
	memset(Val,0,strlen(Temp)+0x10);
	if(strstr(Temp,"0x")==NULL)
		sprintf(Val,"0x%s",Temp);
	else
		memcpy(Val,Temp,strlen(Temp));
	//iResult = atoi(Val);
	sscanf(Val,"0x%x",&iResult);
	return iResult;
}
//---------------------------------------------------------------------------
bool MyIniManager::ReadBool(char* szSection, char* szKey, bool bolDefaultValue)
{
	char szResult[255];
	wchar_t wszResult[255];
	char szDefault[255];
	bool bolResult;
	sprintf(szDefault, "%s", bolDefaultValue? "True" : "False"); 
#ifdef UNICODE	
	wmemset(wszResult,0,255);
	GetPrivateProfileString(char_to_wchar(szSection),  char_to_wchar(szKey), 
					char_to_wchar(szDefault),wszResult,255, char_to_wchar(FFileName));
	memset(szResult,0,255);					
	char *Tmp = wchar_to_char(wszResult);
	memcpy(szResult,Tmp,strlen(Tmp));
#else
	GetPrivateProfileString(szSection, szKey, szDefault, szResult, 255, FFileName);
#endif		 	
 bolResult =  (strcmp(szResult, "True") == 0 ||
		strcmp(szResult, "true") == 0) ? true : false;
 return bolResult;
}
//---------------------------------------------------------------------------
char* MyIniManager::ReadString(char* szSection, char* szKey, const char* szDefaultValue)

{
	wchar_t wszResult[255];	
	char* szResult = new char[255];
	memset(szResult, 0x00, 255);
	memset(wszResult,0,sizeof(wszResult));
#ifdef UNICODE	
	//LSection = char_to_wchar(szSection);
	GetPrivateProfileString(char_to_wchar(szSection),  char_to_wchar(szKey),
					char_to_wchar((char*)szDefaultValue),wszResult,255, char_to_wchar(FFileName));
	char *Tmp = wchar_to_char(wszResult);
	memcpy(szResult,Tmp,strlen(Tmp));
#else
 GetPrivateProfileString(szSection,  szKey,
		szDefaultValue, szResult, 255, FFileName);
#endif		 
 return szResult;
}
//---------------------------------------------------------------------------
wchar_t* MyIniManager::char_to_wchar(char* Input)
{
	if(Input != NULL)
	{
		wchar_t *Tmp = (wchar_t*)malloc(strlen(Input)*sizeof(wchar_t) + 4);
		wmemset(Tmp,0,strlen(Input)+1);
		mbstowcs(Tmp,Input,strlen(Input));
		return Tmp;
	}
	return NULL;
}
//---------------------------------------------------------------------------
char* MyIniManager::wchar_to_char(wchar_t* Input)
{
	if(Input != L"")
	{
		char *Tmp = (char*)malloc(wcslen(Input)+0x10);
		memset(Tmp,0,wcslen(Input)+0x10);
		wcstombs(Tmp,Input,wcslen(Input));
		return Tmp;
	}
	return NULL;
}
//---------------------------------------------------------------------------
list<char*> MyIniManager::GetSectionNames()
{
	int MaxData = 0x1000;
	list <char*> Out;
	int Index=0;
	char *Txt;
	int Len;	
#ifdef UNICODE
	wchar_t *Tmp = (wchar_t*)malloc(MaxData*sizeof(wchar_t));
	GetPrivateProfileSectionNames(Tmp,0x1000,char_to_wchar(FFileName));
	while(Index<MaxData)
	{
		Len = wcslen(Index+Tmp);
		if(Len)
		{
			Txt = (char*)malloc(Len + 1);
			memset(Txt,0,Len + 1);
			wcstombs(Txt,Index+Tmp,Len);
			Out.push_back(Txt);
			Index += Len + 1;
		}
		else
			Index=MaxData;
	}
#else
	char *Tmp=(char*)malloc(MaxData);
	GetPrivateProfileSectionNames(Tmp,0x1000,FFileName);
	while(Index<MaxData && Tmp[Index] != 0x00)
	{
		Len = strlen(Tmp+Index);
		if(Len)
		{
			Txt = (char*)malloc(Len + 1);
			memset(Txt, 0, Len + 1);
			memcpy(Txt, Tmp + Index, Len);
			Out.push_back(Txt);
			Index+=Len+1;
		}
		else
			Index = MaxData;
	}
#endif	
	return Out;
}
//---------------------------------------------------------------------------
int MyIniManager::GetVersion()
{
	return MYINIMGR_VER10;
}
//---------------------------------------------------------------------------
list <KeyItem*> MyIniManager::GetSectionKeys(char *Section)
{
	list <KeyItem*> Out;	
	int MaxData = 0x1000;	
	int KeyLen,ValueLen;
	KeyItem *Item;
	int Index = 0;
	char *KeyTxt,*TmpChar;
	int Len;
#ifdef UNICODE
	wchar_t *Data = (wchar_t*)malloc(MaxData*sizeof(wchar_t));
	wchar_t *WTmp = char_to_wchar(Section);
	wchar_t *WSection = (wchar_t*)malloc(wcslen(WTmp)+4);
	wmemset(WSection,0,wcslen(WTmp)+4);
	swprintf(WSection,L"%s",WTmp);
	GetPrivateProfileSection(WSection,Data,MaxData,char_to_wchar(FFileName));
	while(Index<MaxData)
	{
		Len = wcslen(Index+Data);
		if(Len)
		{
			Item = (KeyItem*)malloc(sizeof(KeyItem));
			KeyTxt = (char*)malloc(Len+1);
			memset(KeyTxt,0,Len+1);
			wcstombs(KeyTxt,Index+Data,wcslen(Index+Data));
			TmpChar = strstr(KeyTxt,"=");
			if(TmpChar!=NULL)
			{
				KeyLen = TmpChar - KeyTxt;
				ValueLen = strlen(KeyTxt)- KeyLen - 1;
				Item->Key = (char*)malloc(KeyLen +1);
				Item->Value = (char*)malloc(ValueLen + 1);
				memset(Item->Key,0,KeyLen + 1);
				memset(Item->Value,0,ValueLen + 1);
				memcpy(Item->Key,KeyTxt,KeyLen);
				memcpy(Item->Value, TmpChar + 1, ValueLen);
				Out.push_back(Item);
			}
		}
		else
			Index = MaxData;
		Index += Len + 1;			
	}
#else
	char *Data = (char*)malloc(MaxData);
	memset(Data,0,MaxData);
	GetPrivateProfileSection(Section,Data,MaxData,FFileName);
	while(Index<MaxData && Data[Index] != 0x00)
	{
		Len = strlen(Data+Index);
		if(Len)
		{
			KeyTxt = Data+Index;
			Item = (KeyItem*)malloc(sizeof(KeyItem));		
			TmpChar = strstr(KeyTxt,"=");
			if(TmpChar!=NULL)
			{
				KeyLen = TmpChar - KeyTxt;
				ValueLen = strlen(KeyTxt)- KeyLen - 1;
				Item->Key = (char*)malloc(KeyLen +1);
				Item->Value = (char*)malloc(ValueLen + 1);
				memset(Item->Key,0,KeyLen + 1);
				memset(Item->Value,0,ValueLen + 1);
				memcpy(Item->Key,KeyTxt,KeyLen);
				memcpy(Item->Value, TmpChar + 1, ValueLen);
				Out.push_back(Item);
			}
		}
		else
			Index = MaxData;
		Index+=Len+1;
	}	
#endif	
	return Out;
}
//---------------------------------------------------------------------------
void MyIniManager::DeleteKey(char *Section, char *Key)
{
#ifdef UNICODE
	WritePrivateProfileString(char_to_wchar(Section),char_to_wchar(Key),L"",
					char_to_wchar(FFileName));
#else
	WritePrivateProfileString(Section,Key,NULL,FFileName);
#endif	
}
//---------------------------------------------------------------------------
void MyIniManager::DeleteSection(char *Section)
{
#ifdef UNICODE
	WritePrivateProfileSection (char_to_wchar(Section),L"",
					char_to_wchar(FFileName));
#else
	WritePrivateProfileSection (Section,NULL,FFileName);
#endif	
}
//---------------------------------------------------------------------------
